using System;
using System.Collections.Generic;
using System.Text;
using Evolution.Engine.Cameras;
using Microsoft.Xna.Framework;
using Evolution.Engine.Managers;
using Evolution.Engine.Interfaces;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Evolution.Engine.Utilities;
using Evolution.Engine.Keymapping;
using Evolution.Engine.Items;

namespace Evolution.Engine.Objects
{
    public class Player : BaseObject, Evolution.Engine.Interfaces.IUpdateable, IChildRenderer, ILoadable
    {
        protected SkinnedModel _Model;
        protected PlayerType _Type;
        protected KeyAssignments _Keys;
        protected QuestCollection _Quests = new QuestCollection();
        protected ItemCollection _Items = new ItemCollection();
        protected AttributeCollection _Attributes = new AttributeCollection();

        public KeyAssignments Keys { get { return _Keys; } set { _Keys = value; } }
        public AttributeCollection Attributes { get { return _Attributes; } set { _Attributes = value; } }
        public ItemCollection Items { get { return _Items; } set { _Items = value; } }
        public QuestCollection Quests { get { return _Quests; } set { _Quests = value; } }

        public new Vector3 Position
        {
            get
            {
                return base._Position;
            }

            set
            {
                _Model.Position = value;
                base._Position = value;
            }
        }
        public new Quaternion Rotation
        {
            get
            {
                return base._Rotation;
            }

            set
            {
                _Model.Rotation = value;
                base._Rotation = value;
            }
        }

        public Player(string name, string modelAsset, string shader, PlayerType playerType)
            : base(name)
        {
            _Model = new SkinnedModel(modelAsset, name, "Idle1");
            _Model.SetShader(shader);
            
            _Shader = shader;
            _Position = Vector3.Zero;

            _Model.Position = _Position;
            _Model.Rotation = _Rotation;

            _Type = playerType;
            _Keys = new KeyAssignments(_Type);
        }

        public void SetScale(Vector3 scale)
        {
            _Scale = scale;
            _Model.Scale = scale;
        }
        public void SetCamera(string name, CameraType type)
        {
            CameraManager.Instance.SetCamera(name, type);
        }

        #region IUpdate members
        void Evolution.Engine.Interfaces.IUpdateable.Update(GameTime gameTime)
        {
        }
        public virtual void HandleInput()
        {
        }
        public void WrapMouse()
        {
            MouseState mouseState = Mouse.GetState();
            Viewport vp = GameEngine.Instance.Device.Viewport;
            Point mouse = new Point(mouseState.X, mouseState.Y);

            if (GameEngine.Instance.Graphics.IsFullScreen)
            {
                if (mouseState.X <= 1)
                    mouse.X = vp.X + vp.Width;

                if (mouseState.X >= vp.Width - 1)
                    mouse.X = vp.X;

                if (mouseState.Y <= 1)
                    mouse.Y = vp.Y + vp.Height;

                if (mouseState.Y >= vp.Height - 1)
                    mouse.Y = vp.Y;
            }
            else
            {
                if (mouseState.X <= vp.X)
                    mouse.X = vp.X + vp.Width;

                if (mouseState.X >= vp.X + vp.Width)
                    mouse.X = vp.X;

                if (mouseState.Y <= vp.Y)
                    mouse.Y = vp.Y + vp.Height;

                if (mouseState.Y >= vp.Y + vp.Height)
                    mouse.Y = vp.Y;
            }

            InputManager.Instance.LastMouseLocation = mouse;
            Mouse.SetPosition(mouse.X, mouse.Y);
        }
        #endregion

        #region ILoadable Members

        void ILoadable.LoadContent()
        {
            _Model.LoadContent();
            _Model.LoopAnimation = true;
        }

        #endregion

        #region IChildRenderer Members

        public void RenderChildren(GameTime gameTime)
        {
            if (CameraManager.Instance.ActiveCamera.Type == CameraType.ThirdPerson)
                _Model.Draw(gameTime);
        }

        #endregion

        #region Physics Members

        public void ApplyForce(Vector3 force)
        {
            _PhysicsObject.ApplyForce(force);
        }

        #endregion
    }
}
